<?php

namespace App\Http\Util;

class KeyWord
{
    /**获取关键词来源
     * @return array
     */
  static public function searchWordForm() {

        $referer = isset($_SERVER['HTTP_REFERER'])?$_SERVER['HTTP_REFERER']:'';

        if(strstr( $referer, 'baidu.com')){ //百度

            preg_match( "|baidu.+wo?r?d=([^\&]*)|is", $referer, $tmp );

            $keyword = urldecode( $tmp[1] );

            $from = 'baidu';

        }elseif(strstr( $referer, 'google.com') or strstr( $referer, 'google.cn')){ //谷歌

            preg_match( "|google.+q=([^\&]*)|is", $referer, $tmp );

            $keyword = urldecode( $tmp[1] );

            $from = 'google';

        }elseif(strstr( $referer, 'so.com')){ //360搜索

            preg_match( "|so.+q=([^\&]*)|is", $referer, $tmp );

            $keyword = urldecode( $tmp[1] );

            $from = '360';

        }elseif(strstr( $referer, 'sogou.com')){ //搜狗

            preg_match( "|sogou.com.+query=([^\&]*)|is", $referer, $tmp );

            $keyword = urldecode( $tmp[1] );

            $from = 'sogou';

        }elseif(strstr( $referer, 'soso.com')){ //搜搜

            preg_match( "|soso.com.+w=([^\&]*)|is", $referer, $tmp );

            $keyword = urldecode( $tmp[1] );

            $from = 'soso';

        }else {

            $keyword ='';

            $from = '';

        }

        return array('keyword'=>$keyword,'from'=>$from);

    }
}
